﻿package 
{
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	public class Main extends Sprite{
		private var HighLightLineShape:Shape = new Shape();
		private var LineShapeArr:Array = new Array();
		private var pointArr:Array = new Array();
		private var angleArr:Array = new Array();
		private var msg:TextField = new TextField();
		private var angle:int = 0;
		
		public function Main(){
			drawCircle();//绘制底部的圆
			drawLineShape();//绘制扫描的线
			init_msg_info();
			init_points();
			this.addEventListener(Event.ENTER_FRAME, Run);
		}
		
		//绘制圆形底
		private function drawCircle():void{
			for (var i:int = 0; i < 10; i ++){
				var shape:Shape = new Shape();
				this.addChild(shape);
				shape.x = stage.stageWidth * 0.5;
				shape.y = stage.stageHeight * 0.5;
				shape.graphics.lineStyle(0, 0xffffff, 0.5);
				shape.filters = [new GlowFilter(0x00ff00, 0.5, 1), new BlurFilter(2, 2)];
				if (i < 8) shape.graphics.drawCircle(0, 0, 25 * i + 5);
				else{
					var rad:Number = 25 * (i - 2) + 5;
					var perAnagle:Number = 2 * Math.PI / 8;
					for (var j:int = 0; j < 10; j ++){
						shape.graphics.moveTo(0, 0);
						shape.graphics.lineTo(Math.cos(j * perAnagle) * rad, Math.sin(j * perAnagle) * rad);
					}
				}
			}
		}
		
		private function drawLineShape():void{
			for (var i:int = 0; i < 45; i ++){
				var LineShape = new Shape();
				this.addChild(LineShape);
				LineShape.x = stage.stageWidth * 0.5;
				LineShape.y = stage.stageHeight * 0.5;
				LineShape.graphics.beginFill(0xffffff, 0.1);
				LineShape.filters = [new GlowFilter(0x00ff00, 0.02 * i, 1), new BlurFilter(1, 1)];
				LineShape.graphics.moveTo(0, 0);
				for (var j:int = 0; j < 2; j ++){
					LineShape.graphics.lineTo(Math.cos((i + j - 45) * Math.PI / 180) * 180, Math.sin((i + j - 45) * Math.PI / 180) * 180);
				}
				LineShape.graphics.lineTo(0, 0);
				LineShape.graphics.endFill();
				
				LineShapeArr.push(LineShape);
			}
			
			this.addChild(HighLightLineShape);
			HighLightLineShape.x = stage.stageWidth * 0.5;
			HighLightLineShape.y = stage.stageHeight * 0.5;
			HighLightLineShape.graphics.lineStyle(0, 0xffffff, 1);
			HighLightLineShape.filters = [new GlowFilter(0x00ff00, 1, 1), new BlurFilter(1, 1)];
			HighLightLineShape.graphics.moveTo(0, 0);
			HighLightLineShape.graphics.lineTo(Math.cos(0 * Math.PI / 180) * 180, Math.sin(0 * Math.PI / 180) * 180);
		}
		
		//点初始化
		private function init_points(){
			for(var i : int = 0; i < 10; i ++){
				var rdmR:int = Math.random() * 360 - 180;//角度 
				var rdmD:int = Math.random() * 130 + 45;//距离
				var pointShape = new Shape();
				this.addChild(pointShape);
				pointShape.x = Math.cos(rdmR * Math.PI / 180) * rdmD + stage.stageWidth * 0.5;
				pointShape.y = Math.sin(rdmR * Math.PI / 180) * rdmD + stage.stageHeight * 0.5;
				pointShape.filters = [new GlowFilter(0x00ff00, 0.5, 1), new BlurFilter(5, 5)];
				pointShape.graphics.beginFill(0x00ff00, 1);
				pointShape.graphics.drawCircle(0, 0, 2);
				pointShape.graphics.endFill();
				pointShape.alpha = 0;
				
				pointArr.push(pointShape);
				angleArr.push(rdmR);
			}
		}
		
		//重绘构建扫描时候的状态
		private function redraw():void{
			for(var j : int = 0; j < pointArr.length; j ++){
				var pointShape = pointArr[j];
				if((angle <= 180 && angleArr[j] == angle) || (angle > 180 && angleArr[j] + 360 == angle)) pointShape.alpha = 1;
				else if(pointShape.alpha > 0) pointShape.alpha -= 0.01;
				else if(pointShape.alpha <= 0){
					pointShape.alpha = 0;
					
					var rdmR:int = Math.random() * 360 - 180;//角度
					var rdmD:int = Math.random() * 130 + 45;//距离
					pointShape.x = Math.cos(rdmR * Math.PI / 180) * rdmD + stage.stageWidth * 0.5;
					pointShape.y = Math.sin(rdmR * Math.PI / 180) * rdmD + stage.stageHeight * 0.5;
					
					angleArr[j] = rdmR;
				}
			}
		}
		
		//显示角度信息
		private function init_msg_info():void{
			this.addChild(msg);
			var label:TextField = new TextField();//标签
			label.text = "角度:";
			label.textColor = 0xffffff;
			this.addChild(label);
			label.x = 20;
			label.y = 30;
			msg.textColor = 0xffffff;
			msg.x = 60;
			msg.y = 30;
		}
		
		private function Run(event:Event):void{
			for (var i:int = 0; i < LineShapeArr.length; i ++) LineShapeArr[i].rotation = angle;//角度旋转
			HighLightLineShape.rotation = angle;
			angle ++;
			if (angle == 360) angle = 0;
			redraw();
			
			msg.text = (angle > 180 ? angle - 360 : angle).toString();
		}
	}
}